import 'package:aegis_flutter_sdk/aegis_flutter_sdk.dart';
import 'package:aegis_flutter_sdk/src/pipes/throttle.dart';
import 'package:flutter_test/flutter_test.dart';
import 'fakeAegis.dart';

void main() {
  group('throttle test', () {
    test('reach max length', () {
      FakeAegis ageis = FakeAegis(Config(id: 'testid', delay: 0));
      var throttleFn = createThrottlePipe<NormalLog>(ageis, 0);
      List<NormalLog> logs = [NormalLog(msg: 'test', level: LogType.infoAll)];
      List<NormalLog> logs2 = [NormalLog(msg: 'test', level: LogType.infoAll)];
      throttleFn(logs, (res) {
        expect(res.length, 1);
        expect(res[0].msg, logs[0].msg);
      });
      throttleFn(logs2, (res) {
        expect(res.length, 1);
        expect(res[0].msg, logs2[0].msg);
      });
      // throttleFn(logs2, (res) {

      // })
    });
    test('unreach max length', () {
      FakeAegis ageis = FakeAegis(Config(id: 'testid', delay: 1000));
      var throttleFn = createThrottlePipe<NormalLog>(ageis, 2);
      List<NormalLog> logs = [NormalLog(msg: 'test', level: LogType.infoAll)];
      List<NormalLog> logs2 = [NormalLog(msg: 'test', level: LogType.infoAll)];

      throttleFn(logs, (res) {});
      // 超出max-length limit， 两条log合并上报
      throttleFn(logs2, (res) {
        expect(res.length, 2);
        expect(res[0].msg, logs[0].msg);
        expect(res[1].msg, logs2[0].msg);
      });
      // throttleFn(logs2, (res) {

      // })
    });
  });
}
